set seed 68187

local totalSims = 10000

foreach y in economy energy immigration {
quietly {
postfile buffer sigNone sigLinear sigSemipar sigFE sigRE convergedRE using Working/`y'FalsePosSim, replace

use Data/Pew_Models.dta, clear

 
reg abd_d d_congress year if `y'==1

keep if e(sample)==1

bysort year: egen meanUnit = mean(unit)

local bCongress = _b[d_congress]
local bYear = _b[year]
local bConstant = _b[_cons]
local sdError =  e(rmse)

forvalues x = 1/`totalSims' {
noisily disp `"`y' "'  `x'

gen fake =  `bCongress'*d_congress + `bYear'*year + `bConstant' + rnormal(0, `sdError')


noisily disp "		None" 
reg fake unit d_congress
gen sigNone = ((2 * ttail(e(df_r), abs(_b[unit]/_se[unit])))<0.05)

noisily disp "		Linear" 
reg fake unit d_congress year
gen sigLinear = ((2 * ttail(e(df_r), abs(_b[unit]/_se[unit])))<0.05)

noisily disp "		Semiparametric"
semipar fake unit d_congress, nonpar(year) nograph 
gen sigSemipar = ((2 * ttail(e(df_r), abs(_b[unit]/_se[unit])))<0.05)

noisily disp "		Fixed-effects"
reg fake unit d_congress i.year
gen sigFE = ((2 * ttail(e(df_r), abs(_b[unit]/_se[unit])))<0.05)

noisily disp "		Random-effects"
mixed fake unit d_congress meanUnit || year:,  iterate(100)
gen sigRE = ((2*normal(-abs(_b[unit]/ _se[unit])))<0.05)
gen convergedRE = e(converged) 

mean sigNone sigLinear sigSemipar sigFE sigRE convergedRE

post buffer (_b[sigNone]) (_b[sigLinear]) (_b[sigSemipar]) (_b[sigFE]) (_b[sigRE]) (_b[convergedRE]) 

drop fake sigNone sigLinear sigSemipar sigFE sigRE convergedRE

}

postclose buffer

}
}

use Working/economyFalsePosSim, clear
gen issue = "Economy"
save Working/economyFalsePosSim, replace

use Working/energyFalsePosSim, clear
gen issue = "Energy"
save Working/energyFalsePosSim, replace

use Working/immigrationFalsePosSim, clear
gen issue = "Immigration"
save Working/immigrationFalsePosSim, replace

use Working/economyFalsePosSim, clear
append using Working/energyFalsePosSim
append using Working/immigrationFalsePosSim


mean sigNone sigLinear sigSemipar sigFE sigRE if issue=="Economy"
mean sigNone sigLinear sigSemipar sigFE sigRE if issue=="Energy"
mean sigNone sigLinear sigSemipar sigFE sigRE if issue=="Immigration"


